package com.bua.toothfactory;

import java.io.ByteArrayOutputStream;
import java.util.HashMap;

import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.widget.*;
import com.bua.control.GudingPaint;
import com.bua.control.SingleCheckButton;
import com.bua.datafactory.DataFactory;
import com.bua.datafactory.DesignDetailDataFactory;
import com.bua.net.NetConfig;
import com.bua.net.NetConfig.HttpRequestId;
import com.bua.net.RequestTask;
import com.bua.net.ResponseReport;
import com.bua.net.Tool;
import com.bua.net.obj.CreateGudingOrderObject;
import com.bua.toothfactory.config.Config;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.util.Base64;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import org.json.JSONException;

public class GudingOrderActivity extends Activity implements ResponseReport {


    private SingleCheckButton scbJGS = null;
    private TextView tvDesignId = null;
    private TextView tvCreateDate = null;


    private Button btnCreateDesignOrder = null;

    private EditText etGenbu = null;
    private EditText etZhongbu = null;
    private EditText etShangbu = null;
    private EditText etQieduanci = null;
    private SingleCheckButton scbGDQTSJ = null;
    private SingleCheckButton scbLMJCDXT = null;
    private SingleCheckButton scbGBYSJ = null;
    private SingleCheckButton scbJSJDGSJ = null;

    private SingleCheckButton scbYHGX = null;
    private SingleCheckButton scbJCDSJ = null;
    private SingleCheckButton scbYBLBZS = null;
    private SingleCheckButton scbJYJTWZ = null;
    private SingleCheckButton scbTSYQ = null;
    private CheckBox icbQTJQ = null;


    private LinearLayout llSPaint = null;

    private String designId = null;
    private String orderId = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.guding_order_activity);
        Intent intent = this.getIntent();
        designId = intent.getStringExtra("designId");
        orderId = intent.getStringExtra("orderId");

        tvDesignId = (TextView) findViewById(R.id.tvDesignId);
        scbJGS = (SingleCheckButton) findViewById(R.id.scbJGSSelector);
        scbJGS.items = Config.JGSList;
        tvCreateDate = (TextView) findViewById(R.id.tvCreateDate);

        btnCreateDesignOrder = (Button) findViewById(R.id.btnCreateDesignOrder);
        btnCreateDesignOrder.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                GudingOrderActivity.this.CreateDesignOrder();
            }
        });

        etGenbu = (EditText) findViewById(R.id.etGenbu);
        etZhongbu = (EditText) findViewById(R.id.etZhongbu);
        etShangbu = (EditText) findViewById(R.id.etShangbu);
        etQieduanci = (EditText) findViewById(R.id.etQieduanci);
        scbGDQTSJ = (SingleCheckButton) findViewById(R.id.scbGDQQDSJ);
        scbGDQTSJ.SetItems(Config.guding_gudingqiao);
        scbLMJCDXT = (SingleCheckButton) findViewById(R.id.scbLMJCDXT);
        scbLMJCDXT.SetItems(Config.guding_linmianjiechudian);
        scbGBYSJ = (SingleCheckButton) findViewById(R.id.scbGBYSJ);
        scbGBYSJ.SetItems(Config.guding_guanbianyuan);
        scbJSJDGSJ = (SingleCheckButton) findViewById(R.id.scbJSJDGSJ);
        scbJSJDGSJ.SetItems(Config.guding_jinshujidiguan);

        scbYHGX = (SingleCheckButton) findViewById(R.id.scbYHGX);
        scbYHGX.SetItems(Config.guding_yaoheguanxi);
        scbJCDSJ = (SingleCheckButton) findViewById(R.id.scbJCDSJ);
        scbJCDSJ.SetItems(Config.guding_jiechudiansongjin);
        scbYBLBZS = (SingleCheckButton) findViewById(R.id.scbYBLBZS);
        scbYBLBZS.SetItems(Config.guding_yubeiliangbuzu);
        scbJYJTWZ = (SingleCheckButton) findViewById(R.id.scbJYJTWZ);
        scbJYJTWZ.SetItems(Config.guding_jiyajiantaiweizhi);
        scbTSYQ = (SingleCheckButton) findViewById(R.id.scbTSYQ);
        scbTSYQ.SetItems(Config.guding_teshuyaoqiu);
        icbQTJQ = (CheckBox) findViewById(R.id.icbQTJQ);

        llSPaint = (LinearLayout) findViewById(R.id.llStationaryPaintBackground);
        if (designId == null) {
            GudingPaint paint = new GudingPaint(this);
            llSPaint.addView(paint,1);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.guding_order, menu);
        return true;
    }

    private void CreateDesignOrder() {
        CreateGudingOrderObject OrderObject = new CreateGudingOrderObject();
        OrderObject.genbu = etGenbu.getText().toString();
        OrderObject.zhongbu = etZhongbu.getText().toString();
        OrderObject.shangbu = etShangbu.getText().toString();
        OrderObject.qieduanci = etQieduanci.getText().toString();
        OrderObject.gudingqiaoqiaodisheji = scbGDQTSJ.GetTitle();
        OrderObject.linmianjiechudianxingtai = scbLMJCDXT.GetTitle();
        OrderObject.guanbianyuansheji = scbGBYSJ.GetTitle();
        OrderObject.jinshujidiguansheji = scbJSJDGSJ.GetTitle();
        OrderObject.yaoheguanxi = scbYHGX.GetTitle();
        OrderObject.jiechudiansongjin = scbJCDSJ.GetTitle();
        OrderObject.yubeiliangbuzushi = scbYBLBZS.GetTitle();
        OrderObject.jiyajiantaiweizhi = scbJYJTWZ.GetTitle();
        OrderObject.teshuyaoqiu = scbTSYQ.GetTitle();
        OrderObject.qiaotijianqing = icbQTJQ.isChecked();

        String Image = null;
        llSPaint.setDrawingCacheEnabled(true);
        llSPaint.setBackgroundColor(Color.WHITE);
        Bitmap bmPicture = llSPaint.getDrawingCache();
        Bitmap mBitmap = Bitmap.createBitmap(bmPicture, 280, 0, 720, 222);
        ByteArrayOutputStream pictureOutput = new ByteArrayOutputStream();
        if (mBitmap!=null &&  mBitmap.compress(CompressFormat.JPEG, 50, pictureOutput))
            Image = Base64.encodeToString(pictureOutput.toByteArray(),
                    Base64.DEFAULT);

        String JGSId = scbJGS.GetTitle().split(" ")[0];
        if(JGSId==null || JGSId.length()<=0)
        {
            Tool.showToast(this, "请输入完整信息");
            return;
        }
        String url = NetConfig.creatUrl(HttpRequestId.CREATE_DESIGN_ORDER);
        RequestTask request = new RequestTask();
        HashMap<String, String> requestData = NetConfig.CreateDesignOrder(orderId, "GD", JGSId, Image, OrderObject.CreateOrderObject());
        request.CreateTask(url, requestData, NetConfig.HttpRequestId.CREATE_DESIGN_ORDER.ordinal(), this);

    }


    @Override
    public void callBack(String result, int requestId) {
        //To change body of implemented methods use File | Settings | File Templates.
        NetConfig.HttpRequestId RequestId = NetConfig.HttpRequestId.values()[requestId];
        switch (RequestId) {
            case CREATE_DESIGN_ORDER: {
                DataFactory resultData=new DataFactory(result);
                if(resultData.status==200)
                {
                    try {
                        tvDesignId.setText("设计定单编号：" +resultData.body.getString("designID"));
//                        tvCreateDate.setText(resultData.body.getString("created"));
                        this.finish();
                    }   catch (JSONException e)
                    {

                    }

                }
            }
            break;
            case GET_DESIGN_ORDER_DETAIL: {
                SetDesignOrder(result);
            }
            break;
            default:
                break;
        }
    }

    private void SetDesignOrder(String jsonString) {
        DesignDetailDataFactory obj = new DesignDetailDataFactory(jsonString);
        tvCreateDate.setText("创建日期：" + obj.createdDate);
        tvDesignId.setText("设计定单编号：" + obj.designId);
        scbJGS.setText(obj.jigongsuo);
        btnCreateDesignOrder.setVisibility(View.INVISIBLE);


        etGenbu.setText(obj.GetItem("根部").toString());
        etGenbu.setEnabled(false);
        etZhongbu.setText(obj.GetItem("中部").toString());
        etZhongbu.setEnabled(false);
        etShangbu.setText(obj.GetItem("上部").toString());
        etShangbu.setEnabled(false);
        etQieduanci.setText(obj.GetItem("切端瓷").toString());
        etQieduanci.setEnabled(false);
        scbGDQTSJ.setText(obj.GetItem("固定桥桥底设计").toString());
        scbGDQTSJ.setEnabled(false);
        scbLMJCDXT.setText(obj.GetItem("领面接触点形态").toString());
        scbLMJCDXT.setEnabled(false);
        scbGBYSJ.setText(obj.GetItem("冠边缘设计").toString());
        scbGBYSJ.setEnabled(false);
        scbJSJDGSJ.setText(obj.GetItem("金属基底冠设计").toString());
        scbJSJDGSJ.setEnabled(false);

        scbYHGX.setText(obj.GetItem("咬合关系").toString());
        scbYHGX.setEnabled(false);
        scbJCDSJ.setText(obj.GetItem("接触点松紧").toString());
        scbJCDSJ.setEnabled(false);
        scbYBLBZS.setText(obj.GetItem("预备量不足时").toString());
        scbYBLBZS.setEnabled(false);
        scbJYJTWZ.setText(obj.GetItem("基牙尖台位置").toString());
        scbJYJTWZ.setEnabled(false);
        scbTSYQ.setText(obj.GetItem("特殊要求").toString());
        scbTSYQ.setEnabled(false);
        icbQTJQ.setChecked((Boolean)obj.GetItem("桥体减轻"));


        ImageView image = new ImageView(this);
        image.setImageBitmap(BitmapFactory.decodeByteArray(obj.designImage, 0, obj.designImage.length));
        llSPaint.addView(image,1);
    }
}
